﻿#include "logwin.h"
#include "ui_logwin.h"
#include <QDebug>
#include <QDateTime>
#include <QDir>
#include <qevent.h>
LogWin * LogWin::logWin = NULL;

LogWin::LogWin(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LogWin)
{
    ui->setupUi(this);
    ui->txtLog->document ()->setMaximumBlockCount (1000);
    bPause=false;
    ui->label->setText("Ver 1.0.1");

    // 1. init the logging mechanism
    Logger& logger = Logger::instance();
    logger.setLoggingLevel(QsLogging::TraceLevel);

    // 2. add file&debug destinations
    const QString sLogPath(QDir(QApplication::applicationDirPath()).filePath("log.txt"));
    DestinationPtr fileDestination(DestinationFactory::MakeFileDestination(
    sLogPath, EnableLogRotation, MaxSizeBytes(5*1024*1024), MaxOldLogCount(10)));
    logger.addDestination(fileDestination);
    DestinationPtr debugDestination(DestinationFactory::MakeDebugOutputDestination());
    logger.addDestination(debugDestination);
    // 3. start logging
    //QLOG_INFO() << "Program started";
}

LogWin::~LogWin()
{
    delete ui;
}

LogWin * LogWin::instance()
{
    if (LogWin::logWin == NULL)
    {
        LogWin::logWin = new LogWin();
        LogWin::logWin->setWindowModality(Qt::NonModal );

    }

    return (LogWin::logWin);
}
void LogWin::destroy(bool destroyWindow, bool destroySubWindows)
{
    //QLOG_INFO() << "Program stoped";
    QsLogging::Logger::destroyInstance();
}

void LogWin::log(QString str)
{
    if(bPause)
        return;

    if(ui->txtLog->document()->lineCount() >1000)
        ui->txtLog->clear();

    ui->txtLog->append(str);
}

void LogWin::on_pushButton_clicked()
{
    ui->txtLog->clear();
}

void LogWin::on_pushButton_2_clicked()
{
    bPause=!bPause;
    if(bPause)
        ui->pushButton_2->setStyleSheet("QPushButton{"
                                        " border: 1px solid #D8DAD7;"
                                        " border-radius: 1px;"
                                        " background-color: #0A8B0A;"
                                        "  color:white;}"
                                        "  QPushButton:pressed{"
                                        "  background-color:#EFF2F7;"
                                        "  color:#313D4D;}");
    else
        ui->pushButton_2->setStyleSheet("QPushButton{"
                                        " border: 1px solid #D8DAD7;"
                                        " border-radius: 1px;"
                                        " background-color: #EFF2F7;"
                                        "  color:#313D4D;}"
                                        "  QPushButton:pressed{"
                                        "  background-color:#EFF2F7;"
                                        "  color:#313D4D;}"
                                        );

}
